package com.seelyn.ddvideo.bootstrap;

import com.seelyn.ddvideo.model.VideoPrefix;
import com.seelyn.ddvideo.repository.SystemUserRepository;
import com.seelyn.ddvideo.service.VideoPrefixService;
import com.seelyn.ddvideo.service.VideoService;
import com.seelyn.ddvideo.spider.FromSiteEnum;
import com.seelyn.ddvideo.utils.DownloadManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/14
 */
@Component
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private VideoPrefixService videoPrefixService;

    @Autowired
    private VideoService videoService;
    @Autowired
    private SystemUserRepository userRepository;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        videoPrefixService.deleteAll();
        VideoPrefix prefix = videoPrefixService.findByFromSite(FromSiteEnum.QQ.name());
        if (prefix == null) {
            prefix = new VideoPrefix();
        }
        prefix.setFromSite(FromSiteEnum.QQ.name());
        prefix.setFromSiteName(FromSiteEnum.QQ.getValue());
        prefix.setPrefixUrl("https://api.flvsp.com/?skin=47ks&url=");
        videoPrefixService.saveVideoPrefix(prefix);

        prefix = videoPrefixService.findByFromSite(FromSiteEnum.YOUKU.name());
        if (prefix == null) {
            prefix = new VideoPrefix();
        }
        prefix.setFromSite(FromSiteEnum.YOUKU.name());
        prefix.setFromSiteName(FromSiteEnum.YOUKU.getValue());
        prefix.setPrefixUrl("https://api.47ks.com/webcloud/?v=");
        videoPrefixService.saveVideoPrefix(prefix);

        prefix = videoPrefixService.findByFromSite(FromSiteEnum.IQIYI.name());
        if (prefix == null) {
            prefix = new VideoPrefix();
        }
        prefix.setFromSite(FromSiteEnum.IQIYI.name());
        prefix.setFromSiteName(FromSiteEnum.IQIYI.getValue());
        prefix.setPrefixUrl("https://api.47ks.com/webcloud/?v=");
        videoPrefixService.saveVideoPrefix(prefix);

        prefix = videoPrefixService.findByFromSite(FromSiteEnum.KUYUN.name());
        if (prefix == null) {
            prefix = new VideoPrefix();
        }
        prefix.setFromSite(FromSiteEnum.KUYUN.name());
        prefix.setFromSiteName(FromSiteEnum.KUYUN.getValue());
        prefix.setPrefixUrl("");
        videoPrefixService.saveVideoPrefix(prefix);

        prefix = videoPrefixService.findByFromSite(FromSiteEnum.M3U8.name());
        if (prefix == null) {
            prefix = new VideoPrefix();
        }
        prefix.setFromSite(FromSiteEnum.M3U8.name());
        prefix.setFromSiteName(FromSiteEnum.M3U8.getValue());
        prefix.setPrefixUrl("https://api.47ks.com/webcloud/?type=m3u8&v=");
        videoPrefixService.saveVideoPrefix(prefix);

        prefix = videoPrefixService.findByFromSite(FromSiteEnum.MP4.name());
        if (prefix == null) {
            prefix = new VideoPrefix();
        }
        prefix.setFromSite(FromSiteEnum.MP4.name());
        prefix.setFromSiteName(FromSiteEnum.MP4.getValue());
        prefix.setPrefixUrl("https://api.47ks.com/webcloud/?type=mp4&v=");
        videoPrefixService.saveVideoPrefix(prefix);

        DownloadManager.getInstance().setVideoService(videoService);
        DownloadManager.getInstance().start();


    }


}
